package com.up.vms.application.common.utils;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ZhangSuchao
 * @create 2019/1/12
 * @since 1.0.0
 */
public final class StringUtils {
    private StringUtils() {
    }

    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(14[57])|(17[0])|(17[6])|(17[8])|(17[7])|(17[4])|(17[3])|(18[0,0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static boolean isEmpty(Object str) {
        return str == null || "".equals(str);
    }

    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }

    public static boolean isNotEmpty(String str) {
        return str != null && str.trim().length() > 0;
    }

    public static String nullToStrTrim(String str) {
        if (str == null) {
            return "";
        }
        return str.trim();
    }

    public static boolean checkString(String str, String regex) {
        return str.matches(regex);
    }

    public static int getLength(String str) {
        if (str == null) {
            str = "";
        }
        return str.replaceAll("[^\\x00-\\xff]", "**").length();
    }

    public static boolean checkMD5(String md5) {
        return checkString(md5, "[0-9A-Fa-f]{32}");
    }

    public static String encode(String str) {
        String strEncode = "";
        try {
            if (str != null) {
                strEncode = URLEncoder.encode(str, CommonConstants.UTF_8);
            }
        } catch (UnsupportedEncodingException e) {
        }
        return strEncode;
    }

    public static String decode(String str) {
        String strDecode = "";
        try {
            if (str != null) {
                strDecode = URLDecoder.decode(str, CommonConstants.UTF_8);
            }
        } catch (UnsupportedEncodingException e) {
        }
        return strDecode;
    }

    public static String byte2hex(byte[] b) {
        String str = "";
        String stmp = "";
        int length = b.length;
        for (int n = 0; n < length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1) {
                str += "0";
            }
            str += stmp;
        }
        return str.toLowerCase();
    }

    public static boolean checkEmail(String email) {
        email = nullToStrTrim(email);
        if (isEmpty(email)) {
            return false;
        }
        if (email.length() < 6 || email.length() > 50) {
            return false;
        }
        String regex = "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+";
        regex = "^[a-zA-Z0-9_.-]+@(([a-zA-Z0-9-])+.)+(?:com|cn|net)$";
        final Pattern pattern = Pattern.compile(regex);
        final Matcher mat = pattern.matcher(email);
        if (!mat.find()) {
            return false;
        } else {
            return true;
        }
        // return email.matches(regex);
    }

    public static boolean checkPhone(String phone) {
        return checkString(phone, "1[0-9]{10}");
    }

    public static boolean checkMobile(String mobile) {
        return checkString(mobile, "1[3,4,5,7,8][0-9]{9}");
    }

    public static String addCommonPreFixChar(int str, String preFixStr) {
        DecimalFormat df = new DecimalFormat(preFixStr);
        String str2 = df.format(str);
        return str2;
    }
    //    public static String leftPad(String code, int num, char c) {
    //       return StringUtils.leftPad(code, num, '0');
    //    }
}
